package com.qilu.pe.ui.fragment;

import android.content.Context;
import android.os.Bundle;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.blankj.utilcode.util.SizeUtils;
import com.qilu.pe.R;
import com.qilu.pe.base.BaseResult2;
import com.qilu.pe.dao.bean.ItemSet;
import com.qilu.pe.dao.http.APIRetrofit;
import com.qilu.pe.ui.activity.ItemSetDetailActivity;
import com.qilu.pe.ui.activity.SearchActivity;
import com.ruitu.arad.api.RxUtil;
import com.ruitu.arad.base.BaseFragment;
import com.ruitu.arad.base.base_list.ListBaseAdapter;
import com.ruitu.arad.base.base_list.SuperViewHolder;
import com.ruitu.arad.util.ToastUtils;

import java.util.ArrayList;
import java.util.List;

import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;
import io.reactivex.disposables.Disposable;
import io.reactivex.functions.Consumer;

public class Search2Fragment extends BaseFragment implements SearchActivity.OnSearchClickListener2 {

    private RecyclerView rcv_list;
    private TextView empty_view;
    private List<ItemSet> queryItemList = new ArrayList<>();// 搜索出来的套餐列表
    private QueryItemListAdapter queryItemAdapter;
    private String type = "2";

    public Search2Fragment() {

    }

    public static Search2Fragment newInstance(String type) {
        Search2Fragment fragment = new Search2Fragment();
        Bundle args = new Bundle();
        args.putString("type", type);
        fragment.setArguments(args);
        return fragment;
    }

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        queryItemAdapter = new QueryItemListAdapter(getActivity());
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        View v = inflater.inflate(R.layout.fragment_search, container, false);
        rcv_list = v.findViewById(R.id.rcv_list);
        empty_view = v.findViewById(R.id.empty_view);

        LinearLayoutManager manager = new LinearLayoutManager(getActivity());
        rcv_list.setLayoutManager(manager);
        rcv_list.setAdapter(queryItemAdapter);

        setListeners();

        return v;
    }

    private void setListeners() {
        ((SearchActivity) getActivity()).setOnSearchClickListener2(this);
        queryItemAdapter.setOnItemClickListener(new ListBaseAdapter.OnItemClickListener() {
            @Override
            public void onItemClick(View itemView, int position) {
                Bundle bundle = new Bundle();
                bundle.putString("id", queryItemList.get(position).getId());
                bundle.putString("type", type);
                startActivity(ItemSetDetailActivity.class, bundle);
            }
        });
    }

    @Override
    public void onSearchClick(String word) {
        reqQueryItem(word);
    }

    // 搜索,查询 套餐
    private Disposable reqQueryItem(String name) {
        return APIRetrofit.getDefault().reqSearchItemList(name, type)
                .compose(RxUtil.<BaseResult2<List<ItemSet>>>subIoObMain())
                .subscribe(new Consumer<BaseResult2<List<ItemSet>>>() {
                    @Override
                    public void accept(BaseResult2<List<ItemSet>> listBaseResult2) throws Exception {
                        hideProgress();
                        if (listBaseResult2.isSuccess()) {
                            queryItemList = listBaseResult2.getData();
                        } else {

                        }
                        queryItemAdapter.setDataList(queryItemList);
                        if (isDataEmpty(queryItemList)) {
                            empty_view.setVisibility(View.VISIBLE);
                        } else {
                            empty_view.setVisibility(View.GONE);
                        }
                    }
                }, new Consumer<Throwable>() {
                    @Override
                    public void accept(Throwable throwable) throws Exception {
                        hideProgress();
                        throwable.printStackTrace();
                        ToastUtils.showShort("查询错误");
                        empty_view.setVisibility(View.VISIBLE);
                    }
                });
    }

    // 按照名称检索套餐名称adapter
    private class QueryItemListAdapter extends ListBaseAdapter<ItemSet> {

        public QueryItemListAdapter(Context context) {
            super(context);
        }

        @Override
        public int getLayoutId() {
            return R.layout.item_search_item_list;
        }

        @Override
        public void onBindItemHolder(SuperViewHolder holder, final int position) {
            TextView tv_name = holder.getView(R.id.tv_name);
            TextView tv_price = holder.getView(R.id.tv_price);
            TextView tv_day = holder.getView(R.id.tv_day);
            TextView tv_sig = holder.getView(R.id.tv_sig);
            LinearLayout ll_tag = holder.getView(R.id.vTagContainer);

            ItemSet set = mDataList.get(position);
            tv_name.setText(set.getName());
            tv_price.setText("¥" + set.getPrice());
            tv_day.setText(set.getDay() + "个工作日");
            tv_sig.setText(set.getSignificance());

            ll_tag.removeAllViews();
            for (int i = 0; i < set.getAdapt().size(); i++) {
                ll_tag.addView(createTagView(holder.itemView.getContext(), i, getTag(set.getAdapt().get(i))));
            }
            ll_tag.addView(createTagView(holder.itemView.getContext(), set.getAdapt().size(), set.getDay() + "个工作日"));
        }

        public TextView createTagView(Context context, int index, String tagStr) {
            TextView tag = new TextView(context);
            tag.setHeight(SizeUtils.dp2px(26f));
            tag.setBackgroundResource(R.drawable.shape_special_tag_bg);
            tag.setGravity(Gravity.CENTER);
            tag.setPadding(SizeUtils.dp2px(10), 0, SizeUtils.dp2px(10), 0);
            tag.setTextColor(getResources().getColor(R.color.colorPrimaryDark));
            tag.setTextSize(14f);
            tag.setText(tagStr);
            if (index > 0) {
                LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT);
                params.leftMargin = SizeUtils.dp2px(10f);
                tag.setLayoutParams(params);
            }
            return tag;
        }

        public String getTag(String tagId) {
            //           0全部,1男性,2女性,3儿童,4青年,5中老年
            switch (tagId) {
                case "0":
                    return "全部";
                case "1":
                    return "男性";
                case "2":
                    return "女性";
                case "3":
                    return "儿童";
                case "4":
                    return "青年";
                case "5":
                    return "中老年";
                default:
                    return "";
            }
        }
    }
}